package com.lw.order.controller;

import com.lw.commonutil.result.Result;
import com.lw.commonutil.result.ResultCodeEnum;
import com.lw.order.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/order/payLog")
public class PayLogController {
    @Autowired
    private PayLogService payLogService;
    //生成微信支付二维码
    @GetMapping("/createNative/{orderNo}")
    public Result createNative(@PathVariable String orderNo){
       Map<String,Object> map =  payLogService.createNative(orderNo);
       return Result.ok( map );
    }
    //查询订单支付状态
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo){
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        if (map==null){
            return Result.fail();
        }
        if ("SUCCESS".equals(map.get("trade_state"))){//支付成功
            //支付成功 添加记录到支付记录表 更新订单表中支付状态
            payLogService.updateOrderStatus(map);
            return Result.ok();
        }
        return Result.build(null, ResultCodeEnum.PAY_RUN);//支付中
    }
}
